const db = wx.cloud.database()
const scMap = db.collection("school-map")
const gtMap = db.collection("gate-map")

var db1 = [
  "gate-map",
  "teach-map",
  "scenery-map",
  "college-map",
  "canteen-map",
  "dormitory-map",
  "express-map"
]

var dis = "ALWAYS"


Page({

  data: {
    show: false,
    markers: [],
    activeindex: 0,
    modal: "block",
    coreLongitude: "114.3330440000",
    coreLatitude: "30.5083720000",
    scale: 14,
    isSpread: !0,
    scrollTop: 0,
  },
  regionchange: function (a) {
    var n = this;
    this.mapContext.getScale({
      success: function (a) {
        if (a.scale < 14 && "ALWAYS" == dis) {
          dis = "BYCLICK";
          for (e = 0; e < n.data.markers.length; e++)  n.data.markers[e].callout.display = dis;
          n.setData({
            markers: n.data.markers
          });
        } else if (a.scale >= 14 && "BYCLICK" == dis) {
          dis = "ALWAYS";
          for (var e = 0; e < n.data.markers.length; e++) n.data.markers[e].callout.display = dis;
          n.setData({
            markers: n.data.markers
          });
        }
      }
    });
  },


  changeArrow: function (a) {
    this.setData({
      isSpread: !this.data.isSpread
    });
  },
  setMaker:function(e){
    var mak = [];
    var n = this;
    var datab = db.collection(db1[e]);
    datab.get().then(res => {
      var data = res.data
      for (let i = 0; i < data.length; i++) {
        var temp = {
          iconPath: "/image/location_.png",
          id: data[i]._id,
          index: data[i]._id,
          intro: data[i].intro,
          latitude: data[i].geo.latitude,
          longitude: data[i].geo.longitude,
          width: 25,
          height: 25,
          img: data[i].img,
          callout: {
            display: dis,
            content: data[i].title,
            color: '#1296db',
            padding: 5,
            borderRadius: 5
          }
        };
        mak.push(temp);
      };
      n.setData({
        markers: mak,
        scale: 14,
      });
    })
  },
  onChange: function (e) {
    this.setMaker(e.detail.index)
     
    },
  markerTap: function (e) {
    var id = e.markerId
    this.setData({
      modal: "none",
      show: true,
      img: this.data.markers[id].img,
      intro: this.data.markers[id].intro,
      title: this.data.markers[id].callout.content
    })
  },
  onClickHide: function () {
    this.setData({
      modal: "block",
      show: false
    })
  },
  positiontap: function () {
    var t = this;
    wx.getLocation({
      type: "gcj02",
      success: function (i) {
        t.setData({
          latitude: i.latitude,
          longitude: i.longitude
        });
      },
      fail: function () {
        wx.showToast({
          title: "请检查手机定位是否打开",
          icon: "none",
          duration: 3e3
        });
      }
    });
  },
  openLocation:function(e){
    console.log(e.target.dataset.loc.latitude)
    console.log(e.target.dataset.loc.longitude)
    wx.openLocation({
      latitude: e.target.dataset.loc.latitude,
      longitude: e.target.dataset.loc.longitude,
      name: e.target.dataset.loc.callout.content,
      address: e.target.dataset.loc.callout.content,
      complete: function (e) { }
    });
  },
  onLoad: function (options) {},
  onReady: function () {
    var mak = []
    this.mapContext = wx.createMapContext("map");
    this.setMaker(0)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})